package com.wkbb.basic.controller.open;

import com.github.pagehelper.PageInfo;
import com.wkbb.common.dto.basic.VisitorInviteDto;
import com.wkbb.common.dto.basic.VisitorUserDto;
import com.wkbb.basic.service.VisitorService;
import com.wkbb.common.constants.Constants;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
//import com.wkbb.basic.vo.ValidVisitorDeviceUserVo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author caohongdan
 * @date 2020/8/5
 * @description
 */
@RestController
@Api(description = "访客管理")
@RequestMapping("/basic/{apiVersion}/open/visitor")
@Slf4j
public class VisitorController {
    @Autowired
    private VisitorService visitorService;

    @ApiOperation(value = "添加访客邀约")
    @RequestMapping(value = "/addVisitor", method = RequestMethod.POST)
    public ResultDTO addVisitor(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion") String apiVersion,
            @RequestBody VisitorInviteDto visitorInviteDto
    )throws Exception{
        if(
                null== visitorInviteDto.getInviteList() || null == visitorInviteDto.getVisitType() || null == visitorInviteDto.getVisitTimes() ||
                null ==visitorInviteDto.getBeginTime() || null == visitorInviteDto.getEndTime()
        ){
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }
        try{
            return visitorService.addVisitor(visitorInviteDto);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultDTO<>(ErrorCode.BUSINESS_DATA_GET_ERROR);
        }
    }
    ;

    @ApiOperation(value = "查看邀约列表")
    @RequestMapping(value = "/getVisitorList", method = RequestMethod.GET)
    public ResultDTO<PageInfo<VisitorInviteDto>> getVisitorList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion") String apiVersion,
            @ApiParam(value = "用户id", required = true) @RequestParam(name = "userId", required = true) Integer userId,
            @ApiParam(value = "学校id", required = true) @RequestParam(name = "orgId", required = true) Integer orgId,
            @ApiParam(value = "页码", required = true) @RequestParam(name = "pageNo", required = false) Integer pageNo,
            @ApiParam(value = "每页条数", required = true) @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ) {
        log.info("***********getVisitorList******userId:{},pageNo:{},pageSize:{}}", userId, pageNo, pageSize);
        try {
            if (null == userId ||null == orgId ||  null == pageNo || null == pageSize) {
                return new ResultDTO(ErrorCode.PARAM_MISSING);
            }
            Map<String, Object> params = new HashMap<>(2);
            params.put("orgId", orgId);
            params.put("userId", userId);
            return new ResultDTO<PageInfo<VisitorInviteDto>>(visitorService.getVisitorList(params, pageNo, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO(ErrorCode.SYSTEM_EXCEPTION);
        }
    }
    ;

    @ApiOperation(value = "获取访客邀约的个人信息")
    @RequestMapping(value = "/getVisitUserDetails", method = RequestMethod.GET)
    public ResultDTO<VisitorInviteDto> getVisitUserDetails(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion") String apiVersion,
            @ApiParam(value = "访客用户Id", required = true) @RequestParam(name = "id", required = true) Long visitorId
    ) throws Exception {
        if (null == visitorId) {
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }
        try {
            return new ResultDTO<VisitorInviteDto>(visitorService.getVisitUserDetails(visitorId));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(ErrorCode.BUSINESS_DATA_GET_ERROR);
        }
    }

    ;

    @ApiOperation(value = "添加访客身份证，人脸")
    @RequestMapping(value = "/addVisitorUser", method = RequestMethod.POST)
    public ResultDTO addVisitorUser(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion") String apiVersion,
            @RequestBody VisitorUserDto visitorUserDto
    ) throws Exception {
        if(
                null== visitorUserDto.getIdCard()
        ){
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }
        try {
            return visitorService.addVisitorUser(visitorUserDto);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(ErrorCode.BUSINESS_DATA_GET_ERROR);
        }
    }

    @ApiOperation(value = "取消访客邀约")
    @RequestMapping(value = "/cancelVisitor", method = RequestMethod.GET)
    public ResultDTO cancelVisitor(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion") String apiVersion,
            @ApiParam(value = "id", required = true) @RequestParam(name = "id", required = true) Long id
    ) {
        if(null == id){
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }
        try {
            visitorService.cancelVisitor(id);
            return new ResultDTO<>();
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(ErrorCode.BUSINESS_DATA_GET_ERROR);
        }
    }

    ;

    @ApiOperation(value = "更新邀约列表状态")
    @RequestMapping(value = "/updateVisitorStatus", method = RequestMethod.GET)
    public ResultDTO updateVisitorStatus(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion") String apiVersion
    ) {
        try {
            visitorService.updateVisitorStatus();
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(ErrorCode.BUSINESS_DATA_GET_ERROR);
        }
        return new ResultDTO<>();
    }

    @ApiOperation(value = "删除邀约用户人脸")
    @RequestMapping(value = "/deleteVisitorFace", method = RequestMethod.GET)
    public ResultDTO deleteVisitorFace(
            @ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId", required = true) Long userId
    ) {
        log.info("***********UserController.deleteVisitorFace******name:{},mobile:{}", orgId, userId);
        try {
            visitorService.deleteVisitorFace( orgId, userId);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(ErrorCode.BUSINESS_DATA_GET_ERROR);
        }
        return new ResultDTO<>();
    }

//    @ApiOperation(value = "查询有效的访客列表")
//    @RequestMapping(value = "/getValidVisitorUserList", method = RequestMethod.GET)
//    public ResultDTO<Object> getValidVisitorUserList(
//            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion") String apiVersion,
//            @ApiParam(value = "orgId", required = true) @RequestParam(name = "orgId", required = true) Long orgId
//    ) {
//        try {
//            Map<String, Object> params = new HashMap<>(1);
//            params.put("orgId", orgId);
//           // List<ValidVisitorDeviceUserVo> ValidVisitorUserDtoList = visitorService.getValidVisitorUserList(params);
//           // return new ResultDTO(ValidVisitorUserDtoList);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new ResultDTO<>(ErrorCode.BUSINESS_DATA_GET_ERROR);
//        }
//    }

    @ApiOperation(value = "三方腾讯添加访客邀约")
    @RequestMapping(value = "/addVisitorThird", method = RequestMethod.POST)
    public ResultDTO addVisitorThird(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion") String apiVersion,
            @RequestBody VisitorInviteDto visitorInviteDto
    )throws Exception{

        if(
                null== visitorInviteDto.getInviteList() || null ==visitorInviteDto.getBeginTime() || null == visitorInviteDto.getEndTime()
        ){
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }
        try{
            return visitorService.addVisitorThird(visitorInviteDto);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultDTO<>(ErrorCode.BUSINESS_DATA_GET_ERROR);
        }
    }

    @ApiOperation(value = "访客上报")
    @RequestMapping(value = "/visitorRecord", method = RequestMethod.GET)
    public ResultDTO visitorRecord(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion") String apiVersion,
            @ApiParam(value = "用户Id", required = true) @RequestParam(name = "userId", required = true) Long userId,
            @ApiParam(value = "上报时间", required = true) @RequestParam(name = "attTime", required = true) Long attTime,
            @ApiParam(value = "设备ID", required = true) @RequestParam(name = "deviceCode", required = true) String deviceCode,
            @ApiParam(value = "区域Id", required = false) @RequestParam(name = "areaId", required = false) Long areaId

    ) throws Exception {
        if (null == attTime || null == deviceCode || null == userId) {
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }
        try {
            visitorService.visitorRecord(userId,attTime,deviceCode,areaId);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(ErrorCode.BUSINESS_DATA_GET_ERROR);
        }
        return new ResultDTO<>();
    }


}
